#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#codeby     道长且阻
#email      ydhcui@suliu.net/QQ664284092
#website    http://www.suliu.net


import datetime
from peewee import *
import util
from log import logger

dbpath = util.getres('walle.db')
logger.debug(dbpath)
USERDATA = SqliteDatabase(dbpath)
USERDATA.connect()

class BaseModel(Model):
    class Meta:
        database = USERDATA

class User(BaseModel):
    id = IntegerField(primary_key=True)
    uid = CharField(unique=True)
    #积分
    rank = CharField(default=1)
    #qq号
    uin = CharField(default='')
    #微信号
    wxid = CharField(default='')
    #出生年月日
    birthday = CharField(default='')
    #职业
    occupation = CharField(default='')
    #电话
    phone = CharField(default='')
    #国家
    country = CharField(default='')
    #城市
    city = CharField(default='')
    #个人说明
    personal = CharField(default='')
    #昵称
    nick =  CharField(default='')
    #邮箱
    email = CharField(default='')
    #省份
    province = CharField(default='')
    #性别
    gender = CharField(default='')
    #手机
    mobile = CharField(default='')
    #真实姓名
    rename = CharField(default='')
    #身份证号码
    cardid = CharField(default='')
    createdate = DateTimeField(default=datetime.datetime.now)

class Group(BaseModel):
    id = IntegerField(primary_key=True)
    uin = CharField(unique=True)
    gcode = CharField(default='')
    flag = CharField(default='')
    name = CharField(default='')
    gid = CharField(default='')

    startkey = CharField(default='露露')
    startmsg = CharField(default='哈哈哈哈 超级无敌的露露回来啦~')
    endkey = CharField(default='去吧，皮卡丘')
    endmsg = CharField(default='啊~我还会回来的。。。。')
    users = ForeignKeyField(User)
    createdate = DateTimeField(default=datetime.datetime.now)

class Setting(BaseModel):
    id = IntegerField(primary_key=True)
    username = CharField(unique=True)
    botname = CharField(default='Wali')
    isautore = BooleanField(default=True)
    issyslog = BooleanField(default=True)
    ischatlog = BooleanField(default=True)
    createdate = DateTimeField(default=datetime.datetime.now)

class Userai(BaseModel):
    id = IntegerField(primary_key=True)
    instr = CharField()
    outstr = CharField()
    level = IntegerField(default=0)
    createdate = DateTimeField(default=datetime.datetime.now)

class Aikey(BaseModel):
    id = IntegerField(primary_key=True)
    data = CharField()
    createdate = DateTimeField(default=datetime.datetime.now)

class Joke(BaseModel):
    id = IntegerField(primary_key=True)
    data = TextField()
    type = CharField(default='')
    isread = BooleanField(default=False)
    createdate = DateTimeField(default=datetime.datetime.now)

class Quotes(BaseModel):
    id = IntegerField(primary_key=True)
    data = CharField()
    type = CharField(default='')
    isread = BooleanField(default=False)
    createdate = DateTimeField(default=datetime.datetime.now)


def create_table():
    Setting.create_table(fail_silently=True)
    User.create_table(fail_silently=True)
    Userai.create_table(fail_silently=True)
    Aikey.create_table(fail_silently=True)
    Joke.create_table(fail_silently=True)
    Quotes.create_table(fail_silently=True)
    Group.create_table(fail_silently=True)

create_table()

if __name__=='__main__':
    import util ,json ,time
    def create_Quotes():
        def hito():
            print(time.time())
            data=json.loads(util.get("http://api.hitokoto.us/rand"))
            v,k=data["hitokoto"],data['catname']
            return v,k
        while 1:
            try:
                v,k=hito()
                try:
                    Quotes.get(Quotes.data==v,Quotes.type==k)
                except Quotes.DoesNotExist:
                    Quotes.create(data=v,type=k)
                    print(v,k)
                #input()
            except Exception as e:
                print(e)
    #create_Quotes()









